## R Script Output -------------------------------------------------------------
# Figure 3: Top Firms Lobbying on Immigration, 2017


## Instructions ----------------------------------------------------------------
# Step 1: Adjust MAIN_DIR to where README.txt is located
# Step 2: Run entire script


## setup -----------------------------------------------------------------------
# clean slate
rm(list = ls())
date()

# load packages
pkg <- c("tidyverse",
         "RColorBrewer", 
         "gridExtra", 
         "viridis")

lapply(pkg, require, character.only = TRUE)

# set main directory
MAIN_DIR <- "~/Dropbox/Research/JOP-h1b-replication"

## load data -------------------------------------------------------------------
load(file = paste(MAIN_DIR, "lv-imm-fy-99-17-rep.RData", sep = "/"))


## Figure 3: left panel --------------------------------------------------------
# set number of top firms
n.top.lobby.rep <- 5

# extract top firms
top.bvdid.vec <- lob.img.firms.years.replication %>% 
  filter(year == 2017) %>% 
  arrange(desc(n_img_rep_year), client_name) %>%
  top_n(n.top.lobby.rep, n_img_rep_year) %>%
  pull(client_bvdid)

# two tied for 5th place, drop Zebra Technologies (has smaller lobbying expense)
top.bvdid.vec <- top.bvdid.vec[-6]

# subset and clean names
trend.df.rep <- lob.img.firms.years.replication %>%
  filter(client_bvdid %in% top.bvdid.vec) %>%
  mutate(client_name = case_when(client_bvdid == "IE471706" ~ "ACCENTURE",
                                 client_bvdid == "US133728359" ~ "COGNIZANT",
                                 client_bvdid == "US134090555L" ~ "GOOGLE",
                                 client_bvdid == "US911144442" ~ "MICROSOFT",
                                 client_bvdid == "US941672743" ~ "INTEL"),
         client_name = factor(client_name, 
                              levels = c("MICROSOFT", 
                                         "GOOGLE", 
                                         "COGNIZANT", 
                                         "ACCENTURE", 
                                         "INTEL")))

# set parameters
colors <- c("#377EB8", # MICROSOFT
            "#E41A1C", # GOOGLE
            "#FF7F00", # COGNIZANT
            "#4DAF4A", # ACCENTURE
            "black" # INTEL
            )

shapes <- c(16, 17, 15, 4, 7)

axis.title.size <- 16

# plot
p.top.lobby.rep.trend <- ggplot(trend.df.rep, 
                                aes(x = year, y = n_img_rep_year,
                                    group = client_name,
                                    color = client_name,
                                    shape = client_name)) +
  geom_point(size = 3) +
  geom_line() +
  scale_color_manual(values = colors) +
  scale_shape_manual(values = shapes) +
  scale_y_continuous("Annual IMM Lobbying Reports", 
                     limits = c(0, 65)) +
  scale_x_continuous("Year",
                     breaks = seq(1999, 2017, by = 2),
                     labels = seq(1999, 2017, by = 2),
                     limits = c(1999, 2021)) +
  ggtitle(paste("Top ", n.top.lobby.rep, " Firms Lobbying on IMG", 
                sep = "")) +
  theme_bw() + 
  theme(plot.title = element_text(size = axis.title.size,
                                  face = "bold",
                                  margin = margin(0, 0, 20, 0),
                                  hjust = 0.5),
        axis.title.y = element_text(size = axis.title.size + 1,
                                    margin = margin(0, 5, 0, 0)),
        axis.title.x = element_text(size = axis.title.size,
                                    margin = margin(20, 0, 0, 0)),
        axis.text = element_text(size = axis.title.size - 3),
        panel.grid.minor = element_blank(),
        panel.grid.major.x = element_blank(),
        legend.position = "none")

# add annotation
x.pos <- 2017.4
text.size <- 4.3

# Microsoft
p.top.lobby.rep.trend <- p.top.lobby.rep.trend + 
  annotate("text", 
           x = x.pos, 
           y = trend.df.rep %>%
             filter(year == 2017 & client_name == "MICROSOFT") %>%
             pull(n_img_rep_year),
           label = "MICROSOFT", 
           color = colors[[1]],
           hjust = 0,
           size = text.size)

# GOOGLE
p.top.lobby.rep.trend <- p.top.lobby.rep.trend + 
  annotate("text", 
           x = x.pos, 
           y = trend.df.rep %>%
             filter(year == 2017 & client_name == "GOOGLE") %>%
             pull(n_img_rep_year),
           label = "GOOGLE", 
           color = colors[[2]],
           hjust = 0,
           size = text.size)

# COGNIZANT
p.top.lobby.rep.trend <- p.top.lobby.rep.trend + 
  annotate("text", 
           x = x.pos, 
           y = trend.df.rep %>%
             filter(year == 2017 & client_name == "COGNIZANT") %>%
             pull(n_img_rep_year),
           label = "COGNIZANT", 
           color = colors[[3]],
           hjust = 0,
           size = text.size)

# ACCENTURE
p.top.lobby.rep.trend <- p.top.lobby.rep.trend + 
  annotate("text", 
           x = 2016.5, 
           y = 5,
           label = "ACCENTURE", 
           color = colors[[4]],
           hjust = 0,
           size = text.size)

# INTEL
p.top.lobby.rep.trend <- p.top.lobby.rep.trend + 
  annotate("text", 
           x = 2010, 
           y = 20,
           label = "INTEL", 
           color = colors[[5]],
           hjust = 0,
           size = text.size)

# save
pdf(paste(MAIN_DIR, "Figure-3-left.pdf", sep = "/"), 
    width = 6, height = 6)
p.top.lobby.rep.trend
dev.off()


## Figure 3: right panel -------------------------------------------------------
# set number of top firms
n.top.lobby.exp <- 5

# extract top firms
top.bvdid.vec.exp <- lob.img.firms.years.replication %>% 
  filter(year == 2017) %>% 
  arrange(desc(est_img_expense_fy), client_name) %>%
  top_n(n.top.lobby.exp, est_img_expense_fy) %>%
  pull(client_bvdid)

# subset and clean names
trend.df.exp <- lob.img.firms.years.replication %>%
  filter(client_bvdid %in% top.bvdid.vec.exp) %>%
  mutate(client_name = case_when(client_bvdid == "US201665019" ~ "FACEBOOK",
                                 client_bvdid == "US134090555L" ~ "GOOGLE",
                                 client_bvdid == "US911144442" ~ "MICROSOFT",
                                 client_bvdid == "US270000798" ~ "COMCAST",
                                 client_bvdid == "US911646860" ~ "AMAZON"),
         client_name = factor(client_name, 
                              levels = c("FACEBOOK", 
                                         "GOOGLE", 
                                         "MICROSOFT", 
                                         "COMCAST", 
                                         "AMAZON"))) %>%
  mutate(est_img_expense_fy_mil = est_img_expense_fy / 1000000)

# set parameters
colors.exp <- c("#FF7F00", # FACEBOOK
                "#E41A1C", # GOOGLE
                "#377EB8", # MICROSOFT
                "#4DAF4A", # COMCAST
                "black" # AMAZON
                )

shapes.exp <- c(15, 
                17, 
                16, 
                4, 
                7)

axis.title.size <- 16

# plot
p.top.lobby.exp.trend <- ggplot(trend.df.exp, 
                                aes(x = year, 
                                    y = est_img_expense_fy_mil,
                                    group = client_name,
                                    color = client_name,
                                    shape = client_name)) +
  geom_point(size = 3) +
  geom_line() +
  scale_color_manual(values = colors.exp) +
  scale_shape_manual(values = shapes.exp) +
  scale_y_continuous("Estimated IMM Lobbying Expenses (millions)") +
  scale_x_continuous("Year",
                     breaks = seq(1999, 2017, by = 2),
                     labels = seq(1999, 2017, by = 2),
                     limits = c(1999, 2021)) +
  ggtitle(paste("Top ", n.top.lobby.exp, " Firms Lobbying on IMG", 
                sep = "")) +
  theme_bw() + 
  theme(plot.title = element_text(size = axis.title.size,
                                  face = "bold",
                                  margin = margin(0, 0, 20, 0),
                                  hjust = 0.5),
        axis.title.y = element_text(size = axis.title.size + 1,
                                    margin = margin(0, 5, 0, 0)),
        axis.title.x = element_text(size = axis.title.size,
                                    margin = margin(20, 0, 0, 0)),
        axis.text = element_text(size = axis.title.size - 3),
        panel.grid.minor = element_blank(),
        panel.grid.major.x = element_blank(),
        legend.position = "none")

# add annotation
x.pos <- 2017.4
text.size <- 4.3

# FACEBOOK
p.top.lobby.exp.trend <- p.top.lobby.exp.trend + 
  annotate("text", 
           x = x.pos, 
           y = trend.df.exp %>%
             filter(year == 2017 & client_name == "FACEBOOK") %>%
             pull(est_img_expense_fy_mil) + 0.07,
           label = "FACEBOOK", 
           color = colors.exp[[1]],
           hjust = 0,
           size = text.size)

# GOOGLE
p.top.lobby.exp.trend <- p.top.lobby.exp.trend + 
  annotate("text", 
           x = x.pos, 
           y = trend.df.exp %>%
             filter(year == 2017 & client_name == "GOOGLE") %>%
             pull(est_img_expense_fy_mil) - 0.04,
           label = "GOOGLE", 
           color = colors.exp[[2]],
           hjust = 0,
           size = text.size)

# MICROSOFT
p.top.lobby.exp.trend <- p.top.lobby.exp.trend + 
  annotate("text", 
           x = x.pos, 
           y = trend.df.exp %>%
             filter(year == 2017 & client_name == "MICROSOFT") %>%
             pull(est_img_expense_fy_mil),
           label = "MICROSOFT", 
           color = colors.exp[[3]],
           hjust = 0,
           size = text.size)

# COMCAST
p.top.lobby.exp.trend <- p.top.lobby.exp.trend + 
  annotate("text", 
           x = x.pos, 
           y = trend.df.exp %>%
             filter(year == 2017 & client_name == "COMCAST") %>%
             pull(est_img_expense_fy_mil) - 0.04,
           label = "COMCAST", 
           color = colors.exp[[4]],
           hjust = 0,
           size = text.size)

# AMAZON
p.top.lobby.exp.trend <- p.top.lobby.exp.trend + 
  annotate("text", 
           x = x.pos, 
           y = trend.df.exp %>%
             filter(year == 2017 & client_name == "AMAZON") %>%
             pull(est_img_expense_fy_mil),
           label = "AMAZON", 
           color = colors.exp[[5]],
           hjust = 0,
           size = text.size)

# save
pdf(paste(MAIN_DIR, "Figure-3-right.pdf", sep = "/"), 
    width = 6, height = 6)
p.top.lobby.exp.trend
dev.off()

